package com.lantosec.hmrp.modules.archives.controller;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.util.UUIDUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.archives.entity.UserQualification;
import com.lantosec.hmrp.modules.archives.service.IUserQualificationService;
import com.lantosec.hmrp.modules.archives.vo.UserQualificationVO;

import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * 执业资格信息表 控制器
 *
 * @author AutoCode
 * @since 2020-07-13
 */
@Api(value = "执业资格信息表控制器", tags = "执业资格信息表控制器")
@RestController
@RequestMapping("/archives/user/qualification")
@Log4j2
public class UserQualificationController extends BaseController {
    @Autowired
    private IUserQualificationService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<UserQualificationVO>> all(@ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId") String userId){
        List<UserQualificationVO> userExpertise = service.getAll(userId);
        return Result.ok(userExpertise);
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<UserQualificationVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<UserQualification> page,
                                                  @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId") String userId){
        page = service.page(page,new QueryWrapper<UserQualification>().eq(UserQualification.ColName.user_id.toString(),userId));
        return Result.ok(entity2VO(page));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<UserQualificationVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "执业资格信息表VO对象", required = true) @RequestBody @Validated UserQualificationVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        UserQualification entity = vo.convertToEntity();
        entity.setUserQualificationId(UUIDUtils.get());
        service.save(entity);
        return Result.ok(entity.getUserQualificationId());
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "执业资格信息表VO对象", required = true) @RequestBody UserQualificationVO vo){
        UserQualification entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
